# -*- coding:UTF-8 -*-
import requests
import csv
import random
import time
import socket
import http.client
import json

from requests.adapters import HTTPAdapter
# 请求url获取内容


def get_content(url, data=None):

    timeout = random.choice(range(80, 180))
    while True:
        try:
            sess = requests.Session()
            sess.mount('http://', HTTPAdapter(max_retries=10))
            sess.mount('https://', HTTPAdapter(max_retries=10))
            sess.keep_alive = False  # 关闭多余连接

            header = {
                'Accept': '*/*',
                'Content-Type': 'application/json',

            }
            rep = requests.post(url, json=data, headers=header,
                                timeout=timeout)
            rep.encoding = 'utf-8'
            # req = urllib.request.Request(url, data, header)
            # response = urllib.request.urlopen(req, timeout=timeout)
            # html1 = response.read().decode('UTF-8', errors='ignore')
            # response.close()
            break
        # except urllib.request.HTTPError as e:
        #         print( '1:', e)
        #         time.sleep(random.choice(range(5, 10)))
        #
        # except urllib.request.URLError as e:
        #     print( '2:', e)
        #     time.sleep(random.choice(range(5, 10)))
        except socket.timeout as e:
            print('3:', e)
            time.sleep(random.choice(range(8, 15)))

        except socket.error as e:
            print('4:', e)
            time.sleep(random.choice(range(20, 60)))

        except http.client.BadStatusLine as e:
            print('5:', e)
            time.sleep(random.choice(range(30, 80)))

        except http.client.IncompleteRead as e:
            print('6:', e)
            time.sleep(random.choice(range(5, 15)))
    rep.close()
    return rep.text
    # return html_text

    # 写入文件


def write_data(data, name):
    file_name = name
    with open(file_name, 'a', errors='ignore', newline='') as f:
        f_csv = csv.writer(f)
        f_csv.writerows(data)


if __name__ == '__main__':
    data = {
        "useNew": False,
        "pageModuleId": "262343999128474134",
        "pageContentId": "261151175292879126",
        "categoryId": "610182025473032",
        "storeInfoList": [
            {
                "storeId": "6518",
                "storeType": 2
            },
            {
                "storeId": "6769",
                "storeType": 4
            },
            {
                "storeId": "9999",
                "storeType": 8
            }
        ],
        "moduleDataNum": 3,
        "moduleDataSize": 20,
        "modulePagination": True,
        "uid": "181811268577",
        "appId": "wxb344a8513eaaf849",
        "saasId": "1818"
    }
    url = 'https://api-sams.walmartmobile.cn/api/v1/sams/decoration/portal/show/getPageModuleData'
    result = get_content(url, data)
    jsonData = json.loads(result)
    goodList = jsonData['data']['pageModuleVOList'][0]['renderContent']['categories'][0]['goodsList']
    final = []
    for item in goodList :
        temp=[]
        temp.append(item['title'])
        temp.append(item['image'])
        temp.append(item['spuStockQuantity'])
        temp.append(item['priceInfo'])
        final.append(temp)
    write_data(final, 'products.csv')